Page({
  //全部问题是questions,当前页面的全部内容是current_page,乱序排列是arrays,选项乱序是optionArray
  data: {
    questions: [],
    current_index: 0,
    current_page: null,
    lastPage:0,
    options:[],
    arrays:[],
    option:['A','B','C','D','E','F','G'],
    optionArray:[]
  },

  //随机排序函数
  casualSort: function ()
  {
    return Math.random() > 0.5 ? -1 : 1;
  },

  //数组乱序
  setList: function () {
    let array = [];
    for (let i = 0; i < this.data.questions.length; i++) {
      array.push(i);
    }
    let newList = array.sort(this.casualSort);
    this.setData({
      arrays: newList,
    })
  },

 //选项乱序
  setOptions:function(){
    let array = [];
    for(let i = 0;i<this.data.options.length;i++){
      array.push(i)
    }
    this.setData({
      optionArray: array.sort(this.casualSort)
    })
    console.log(array);
  },
  

  onLoad: function (options) {
    //请求数据
    wx.request({
      url: 'http://120.78.173.208:8080/json_String.php',
      method: 'GET',
      success(res) {
        //处理请求的数据
        var data;
        if (typeof res.data === 'string') {
          data = JSON.parse(res.data.trim())
          console.log('数组',Object.prototype.toString.call(data));
        }else{
          data=res.data;
        }
        
        wx.setStorageSync('questionList', data);

        console.log('数据',data)
      }
    })
    
    this.setData({
      questions:wx.getStorageSync('questionList')
    })
    this.setList();

    //当前页面的问题和选项内容
    let text = this.data.questions[this.data.arrays[this.data.current_index]]

    this.setData({
      current_page: text,
      options: text.options,
      lastPage: this.data.arrays.length
    })

    //选项乱序
    this.setOptions()

  },


  onReady: function () {
  },

 

  next:function(e){

    var self = this;
    self.data.questions[self.data.arrays[self.data.current_index]].userchoose = e.currentTarget.id
    console.log('选择选项：', self.data.questions[self.data.arrays[self.data.current_index]].userchoose)
    if (self.data.current_index + 1 >= self.data.lastPage) {
    
      try {
        wx.setStorageSync('questions', self.data.questions)
      } catch (e) {
        console.log('存储数据失败');
      }
      //跳转页面
      wx.redirectTo({
        url: '../result/result'
      })
    }else{
  //点击加载下一道题的内容
      let index = this.data.current_index +1
      let contentText = this.data.questions[this.data.arrays[index]]
      // console.log(contentText)
      this.setData({
        current_page:contentText,
        current_index:index,
        options: contentText.options
      })
      this.setOptions()
      // console.log(this.data.options)
      // console.log(this.data.optionArray)
    }
   }
})

